package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.Cart;
import com.cskaoyan.bean.User;
import com.cskaoyan.bean.cart.bo.*;
import com.cskaoyan.bean.cart.vo.CartCheckoutData;
import com.cskaoyan.bean.cart.vo.CartData;
import com.cskaoyan.bean.goodsbean.goodsbo.UpdateGoodsBO;
import com.cskaoyan.service.CartService;
import com.fasterxml.jackson.databind.ser.Serializers;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.List;

/**
 * HJL
 * wx购物车，需要从shiro中获得用户id
 */
@RestController
@RequestMapping("wx/cart")
public class WxCartController {

    int fakeUserId = 1;//shiro搭建好之前开发用

    @Autowired
    private CartService cartService;
    /**
     * 开发阶段没有shiro里的用户数据用，有了之后不影响
     * @return
     */
    private Integer getUserIdWhenDevelopping() {
        Integer userId = null;
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if (user != null) {
            userId = user.getId();
        }
        userId = userId == null ? fakeUserId : userId;
        return userId;
    }

    /**
     * 查询用户购物车统计信息
     * @return
     */
    @GetMapping("index")
    public BaseRespVo cartIndex() {
        Integer userId = getUserIdWhenDevelopping();
        CartData cartData = cartService.getUserCartIndex(userId, null);
        return BaseRespVo.ok(cartData);
    }

    /**
     * 查询用户购物车商品数量
     * @return
     */
    @GetMapping("goodscount")
    public BaseRespVo cartGoodsCount() {
        Integer userId = getUserIdWhenDevelopping();
        int count = cartService.cartGoodsCount(userId);
        return BaseRespVo.ok(count);
    }

    /**
     * 添加到购物车
     *
     * @param bo
     * @return
     */                 //仅提示库存不足
    @PostMapping("add")//1 确认库存 2 添加 3 减库存 4 返回购物车件数
    public BaseRespVo addToCart(@Validated @RequestBody AddToCartBo bo, BindingResult bindingResult) throws SQLException {
        Integer userId = getUserIdWhenDevelopping();
        cartService.addToCart(userId, bo);
        //添加失败会抛异常 处理
        int count = cartService.cartGoodsCount(userId);
        return BaseRespVo.ok(count);
    }

    /**
     * 删除购物车的商品
     * @return
     */
    @PostMapping("delete")
    public BaseRespVo deleteFromCart(@RequestBody DeleteFromCartBo bo) throws SQLException {
        List<Integer> productIds = bo.getProductIds();
        Integer userId = getUserIdWhenDevelopping();
        cartService.deleteFromCart(userId, productIds);
        CartData cartData = cartService.getUserCartIndex(userId, null);
        return BaseRespVo.ok(cartData);
    }

    /**
     * 勾选/取消勾选 购物车条目
     * 参数是数组（集合） 说明能多项 勾选，但是前端只传1个
     * @param bo
     * @return
     */
    @PostMapping("checked")
    public BaseRespVo checkCartItems(@RequestBody CheckCartItemBo bo) {
        Integer userId = getUserIdWhenDevelopping();
        cartService.checkCartItems(userId, bo);
        CartData cartData = cartService.getUserCartIndex(userId, null);
        return BaseRespVo.ok(cartData);
    }

    /**
     * 修改购物车条目数量
     * @param bo
     * @return
     */                 //传入的购物车id暂时没什么用，应为用户id+productId唯一确认一条记录 直接复用以前的方法
    @PostMapping("update") //1 把数量变化 转化为 addToCart方法 复用
    public BaseRespVo updateCartItem(@Validated @RequestBody UpdateCartItemBo bo, BindingResult bindingResult) throws SQLException {
        Integer userId = getUserIdWhenDevelopping();
        cartService.updateCartItem(userId, bo);
        //失败会抛异常的
        return BaseRespVo.ok();
    }

    /**快速添加，实质就是个添加动作，前端直接跳转到下单页面而已
     * 如果购物车已经有这个产品，修改数量，以此为准，否则新增
     * @return
     */
    //转化为复用 addToCart方法
    @PostMapping("fastadd")
    public BaseRespVo fastAdd(@Validated @RequestBody AddToCartBo bo, BindingResult bindingResult) throws SQLException {
        Integer userId = getUserIdWhenDevelopping();
        Cart cartAdded = cartService.fastAdd(userId, bo);
        return BaseRespVo.ok(cartAdded.getId());
    }

    /**公网关于 团购 优惠券 运费的 都没有实现
     * 下单前确认信息，团购，优惠卷，地址 运费....所有有勾选的购物车条目 统计计算 ...
     * @param bo
     * @return
     */
    @GetMapping("checkout")
    public BaseRespVo checkout(CartCheckoutBo bo) throws Throwable {
        Integer userId = getUserIdWhenDevelopping();
        CartCheckoutData cartCheckoutData = cartService.cartCheckout(userId, bo);
        return BaseRespVo.ok(cartCheckoutData);
    }
}
